<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class EditOrderController extends Controller
{
    public function index(Request $request)
    {
        $order=$request->except('_token');
        $order['total']=$order['gprice']*$order['number'];
        $address=DB::table('address')->where('uid','=',session()->get('homeuser')->id)->get();
        $config=DB::table('config')->get();
        $id=DB::table('order')->insertGetId($order);
        if($id>0){
        return view('home.editorder',['oid'=>$id,'address'=>$address,'config'=>$config,'order'=>$order]);
        }
    }
    public function store(Request $request)
    {
        $info=$request->input('did');
        $data=array('did'=>$info);
        $oid=$request->input('oid');
        $row = DB::table('order')->where('id',$oid)->update($data);
        $add=DB::table('address')->where('id',$info)->first();
        if($row>0){
            echo json_encode($add);
        }
    }
    public function update($id,Request $request)
    {
        $data=array('uid'=>$request->input('uid'),'state'=>1);
        $row=DB::table('order')->where('id',$id)->update($data);
        if($row>0){
            $row1=DB::table('order')->where('uid',null)->count();
            if ($row1>0) {
                $row2=DB::table('order')->where('uid',null)->delete();
            }
            $list=DB::table('type')->get();
            $config=DB::table('config')->get();
            $dingdan=DB::table('order')->where('id',$id)->get()->toArray();
            return view('home.pay',['list'=>$list,'config'=>$config,'dingdan'=>$dingdan]);
        }
    }
}
